/**
 * Friendly Interactive Recursion Educator (FIRE)
 * COMS 4115: Programming Languages & Translators
 * Prof. Aho
 * 
 * @author Brian Lu
 * @author Sahar Hasan
 * @author Steve Hehl
 * @author Tristan Naumann
 * @version 2009-05-12
 */

import java.io.*;
import java.util.*;
import org.antlr.runtime.*;
import org.antlr.stringtemplate.*;

public class Test {
    
    /**
     * Code comparison for test cases and target output.  Note: a test case can
     * pass code comparison without necessarily being a valid file; target
     * output should be individually validated before running.
     * 
     * @param args
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        FileReader groupFile = new FileReader("Fire.stg");
    	StringTemplateGroup templates = new StringTemplateGroup(groupFile);
    	groupFile.close();
        
        // Generate list of test cases
        File casesDir = new File("cases/");
        String[] cases = casesDir.list();
        
        // Automatically loop through every test case
        for (String s : cases) {
            // Pull the particular test case
            String input = "cases/" + s;
            FireLexer lexer = new FireLexer(new ANTLRFileStream(input));
            WSFilter filter = new WSFilter(lexer);
            CommonTokenStream tokens = new CommonTokenStream(filter);
            
            // Pull the corresponding target file
            Scanner targetFile = new Scanner(new File("output/" + s + ".java"));
            String target = "";
            while (targetFile.hasNext())
                target += (targetFile.nextLine() + "\n");
            
            // Translate the test case
            FireParser parser = new FireParser(tokens);
            parser.setTemplateLib(templates);
            FireParser.s_return r = parser.s();
            StringTemplate program = (StringTemplate) r.getTemplate();
            
            // Compare the translated test case to the target file
            if (equiv(program.toString(), target))
                System.out.println("PASS: " + s);
            else
                System.out.println("FAIL: " + s);
            
            // Generates output files on first run so that they can be compared to later
            // FileWriter fstream = new FileWriter("output/" + s + ".java");
            // BufferedWriter out = new BufferedWriter(fstream);
            // out.write(program.toString());
            // out.close();
        }
    }
    
    /**
     * Determines whether generated output and desired output are identical,
     * whitespace ignored.
     * 
     * @param translated Generated output
     * @param target Desired output
     * @return boolean on equivalence
     */
    public static boolean equiv(String translated, String target) {
        Scanner a = new Scanner(translated);
        Scanner b = new Scanner(target);
        
        while (a.hasNext())
            if (!a.next().equals(b.next()))
                return false;
        
        return true;
    }
    
}